Prepare Interview

Mock Exams

Make Homepage

Bookmark this page

Subscribe Email Address

Java Mail Interview Questions and Answers

Question: Sample code for Reading Multipart mail using JavaMail.
Answer: These days Multipart mail is used to compose emails with attachment. In the email you can attach images, zip files, xls, doc etc.. It allows you to create nicely design emails.

Java Mail API also provides classes to create, send and read the Multipart message. If you have given a task to create emails with attachment in Java technologies, then you can use the Java Mail api to accomplish your task.

Using the Multipart Class

Following code snippet shows how you can use the Multipart class. You have to create the object of Multipart class and then you can get the body part and compose or read your email.

Multipart multipart = (Multipart) msg[i].getContent();After that create BodyPart object BodyPart bodyPart = multipart.getBodyPart(x);

And then read bodyPart content using getContent() method.


package com.withoutbook.common;

import java.util.*;
import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;

public class ReadMultipartMail {

public static void main(String args[]) throws Exception {

String host = "192.168.10.110";
String username = "arindam";
String passwoed = "arindam";

Properties properties = System.getProperties();
Session session = Session.getDefaultInstance(properties);

Store store = session.getStore("pop3");
store.connect(host, username, passwoed);

Folder folder = store.getFolder("inbox");

if (!folder.exists()) {
System.out.println("No INBOX...");
System.exit(0);
}
folder.open(Folder.READ_WRITE);
Message[] msg = folder.getMessages();

for (int i = 0; i < msg.length; i++) {
System.out.println("------------ Message " + (i + 1) + " ------------");
String from = InternetAddress.toString(msg[i].getFrom());
if (from != null) {
System.out.println("From: " + from);
}

String replyTo = InternetAddress.toString(
msg[i].getReplyTo());
if (replyTo != null) {
System.out.println("Reply-to: " + replyTo);
}
String to = InternetAddress.toString(
msg[i].getRecipients(Message.RecipientType.TO));
if (to != null) {
System.out.println("To: " + to);
}

String subject = msg[i].getSubject();
if (subject != null) {
System.out.println("Subject: " + subject);
}
Date sent = msg[i].getSentDate();
if (sent != null) {
System.out.println("Sent: " + sent);
}

System.out.println();
System.out.println("Message : ");

Multipart multipart = (Multipart) msg[i].getContent();

for (int x = 0; x < multipart.getCount(); x++) {
BodyPart bodyPart = multipart.getBodyPart(x);

String disposition = bodyPart.getDisposition();

if (disposition != null && (disposition.equals(BodyPart.ATTACHMENT))) {
System.out.println("Mail have some attachment : ");

DataHandler handler = bodyPart.getDataHandler();
System.out.println("file name : " + handler.getName());
} else {
System.out.println(bodyPart.getContent());
}
}
System.out.println();
}
folder.close(true);
store.close();
}
}
Is it helpful? Yes No

Most helpful rated by users:

©2024 WithoutBook